import {
    approval,
    createOdCharge,
    deleteOdChargeById,
    getOdChargeById,
    queryChargesForPage,
    totalMoney,
    updateOdCharge,
    batchCreate
} from '../../services/logisticsOrder/orderCharge';
import {message} from 'antd';
import Constants from '../../utils/Constants'

const R = require('ramda');

export default {
    namespace: 'orderCharge',
    state: {
        page: {},
        list: [],
        dataSource: {},
        charges:[],
        feeTotal: {}
    },
    reducers: {
        /**
         * 刷新state
         * 
         * @param state 
         */
        refreshState(state, { payload }) {
            return { ...state, ...payload };
        }
    },
    effects: {
        /**
         * 保存费用
         * 
         * @param call
         */
        *createOdCharge({ payload }, { call }) {
            const res = yield call(createOdCharge, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("费用添加成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 更新费用
         * 
         * @param call
         * @param put
         * @param select
         */
        *updateOdCharge({ payload }, { call, put }) {
            const res = yield call(updateOdCharge, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("费用更新成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         * 根据id删除订单费用
         * 
         * @param id
         */
        *deleteOdChargeById({ payload }, { call }) {
            const res = yield call(deleteOdChargeById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("费用删除成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
        * 根据id获取费用
        * 
        * @param id
        */
        *getOdChargeById({ payload }, { call, put }) {
            if (payload.id) {
                const res = yield call(getOdChargeById, { ...R.omit(['onSuccess', 'onFail'])(payload) });
                if (res && res.data.resultCode === "ok") {
                    yield put({
                        type: 'refreshState',
                        payload: {
                            dataSource: res.data.content
                        }
                    });
                } else {
                    if (payload.onFail) payload.onFail();
                }
            } else {
                yield put({
                    type: 'refreshState',
                    payload: {
                        dataSource: {}
                    }
                });
            }
        },
        /**
         * 费用变更审核
         *
         * @param id
         * @return
         */
        *approval({ payload }, { call, put }) {
            const res = yield call(approval, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                message.info("审批结果已提交！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
        /**
         *计算费用金额
         *
         * @param data
         */
        *totalMoney({ payload }, { call, put }) {
            const res = yield call(totalMoney, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        feeTotal: res.data.content
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },

        /**
         * 	可批量对冲费用分页查询
         * 
         * @param orderNo			订单号
         * @param settlementUnit	结算单位
         * @param beginDate			接单开始日期
         * @param endDate			接单截至日期
         * @param lesseeCode		租户编码
         * @param page				分页对象
         * @return
         */
        *queryChargesForPage({ payload }, { call, put }) {
            const res = yield call(queryChargesForPage, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok") {
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: res.data.content.list,
                        page: res.data.content.page
                    }
                });
            } else {
                if (payload.onFail) payload.onFail();
            }
        },


        /**
         * 选择模板批量新增费用
         * @param payload
         * @param call
         * @param put
         */
        *batchCreate({ payload }, { call, put }) {
            const res = yield call(batchCreate, { ...R.omit(['onSuccess', 'onFail'])(payload) });
            if (res && res.data.resultCode === "ok")
            // {
            //     yield put({
            //         type: 'refreshState',
            //         payload: {
            //             charges: res.data.content.charges,
            //             page: res.data.content.page
            //         }
            //     });
            // }
            // else {
            //     if (payload.onFail) payload.onFail();
            // }
            {
                message.info("费用模板添加成功！");
                if (payload.onSuccess) payload.onSuccess();
            } else {
                if (payload.onFail) payload.onFail();
            }
        },
    },




    subscriptions: {
        setup({ history, dispatch }) {
            return history.listen(({ pathname }) => {
                const menus = JSON.parse(sessionStorage.getItem(Constants.USER_MENU));
                if(menus && !menus.find(item=>item.includes(pathname)) && pathname != '/logout'){
                    document.location.href = "/notAuthorized"
                }
            });
        }
    }
};